//
// Created by Administrator on 24-8-10.
//

#include "W25QXXClass.h"
#include "./usart.h"
#include <string>
#include "W25QXX.h"
#include <cstring>
using namespace std;
W25QXXClass::W25QXXClass() {

}

void W25QXXClass::PrintID() {
    uint8_t *ID;
    BSP_W25Qx_Read_ID(ID);
    //第一位厂商ID固定0xEF,第二位设备ID根据容量不同,具体为：
    //W25Q16为0x14、32为0x15、40为0x12、64为0x16、80为0x13、128为0x17
    if((ID[0] != 0xEF) | (ID[1] != 0x16))
    {
        std::string temp="something wrong in Step1 \r\n";
        HAL_UART_Transmit(&huart1,(uint8_t *)temp.data(),temp.length(),200);
    }
    else
    {
        printf(" W25Qxx ID is : ");
        for(int i=0;i<2;i++)
        {
            printf("0x%02X ",ID[i]);
        }
        printf("\r\n");
    }
}

uint8_t W25QXXClass::InitW25Q() {
  return  BSP_W25Qx_Init();
}

void W25QXXClass::TestReadWrite() {
    BSP_W25Qx_Init();
    BSP_W25Qx_Read_ID(ID);
    //第一位厂商ID固定0xEF,第二位设备ID根据容量不同,具体为：
    //W25Q16为0x14、32为0x15、40为0x12、64为0x16、80为0x13、128为0x17
    if((ID[0] != 0xEF) | (ID[1] != 0x16))
    {
        std::string temp="something wrong in Step1 \r\n";
        HAL_UART_Transmit(&huart1,(uint8_t *)temp.data(),temp.length(),200);
    }
    else
    {
        string idstr="W25Qxx ID is : ";
        HAL_UART_Transmit(&huart1,(uint8_t *)idstr.data(),idstr.length(),200);
        std::string formattedString;
        for(i=0;i<2;i++)
        {
            //printf("0x%02X ",ID[i]);
            char buffer[10]; // 足够大以容纳格式化字符串
            std::sprintf(buffer, "0x%02X ", ID[i]); // 使用 sprintf 格式化
            formattedString += buffer; // 追加到最终字符串

        }
        HAL_UART_Transmit(&huart1, (uint8_t *)formattedString.data(), formattedString.length(), 200);
        //printf("\r\n");
        HAL_UART_Transmit(&huart1,(uint8_t *)"\r\n",2,200);
    }

    if(BSP_W25Qx_Erase_Block(0) == W25Qx_OK)
    {
        //printf(" QSPI Erase Block OK!\r\n");
        std::string message=" QSPI Erase Block OK!\r\n";
        HAL_UART_Transmit(&huart1,(uint8_t *)message.data(),message.size(),200);
    }
    else
    {
        std::string message="something wrong in Step2\\r\\n";
        HAL_UART_Transmit(&huart1,(uint8_t *)message.data(),message.size(),200);
    }
    //printf("something wrong in Step2\r\n");
    /*-Step3- 写数据  ************************************************Step3*/
    for(i =0;i<0x100;i ++)
    {
        wData[i] = i;
        rData[i] = 0;
    }

    if(BSP_W25Qx_Write(wData,0x00,0x100)== W25Qx_OK){
        //printf(" QSPI Write OK!\r\n");
        std::string message=" QSPI Write OK!\\r\\n";
        HAL_UART_Transmit(&huart1,(uint8_t *)message.data(),message.size(),200);
    }

    else{
        // printf("something wrong in Step3\r\n");
        std::string message="something wrong in Step3\\r\\n";
        HAL_UART_Transmit(&huart1,(uint8_t *)message.data(),message.size(),200);
    }

    /*-Step4- 读数据  ************************************************Step4*/
    if(BSP_W25Qx_Read(rData,0x00,0x100)== W25Qx_OK){
        //printf(" QSPI Read ok\r\n\r\n");
        std::string message="QSPI Read ok\\r\\n\\r\\n";
        HAL_UART_Transmit(&huart1,(uint8_t *)message.data(),message.size(),200);
    }else{
        //printf("something wrong in Step4\r\n");
        std::string message="something wrong in Step4\\r\\n";
        HAL_UART_Transmit(&huart1,(uint8_t *)message.data(),message.size(),200);
    }


    {
        //printf("QSPI Read Data : \r\n");
        std::string message="QSPI Read Data : \\r\\n";
        HAL_UART_Transmit(&huart1,(uint8_t *)message.data(),message.size(),200);
    }

    string output="";
    for(i =0;i<0x100;i++)
    {
        //printf("0x%02X  ",rData[i]);

        //printf("0x%02X ",ID[i]);
        char buffer[10]; // 足够大以容纳格式化字符串
        std::sprintf(buffer, "0x%02X ", rData[i]); // 使用 sprintf 格式化
        output += buffer; // 追加到最终字符串

    }

    HAL_UART_Transmit(&huart1,(uint8_t *)output.data(),output.size(),200);

    //printf("\r\n\r\n");
    std::string ret="\r\n\r\n";
    HAL_UART_Transmit(&huart1,(uint8_t *)ret.data(),ret.size(),200);
    /*-Step5- 数据对比  ************************************************Step5*/
    if(memcmp(wData,rData,0x100) == 0 ){
        //printf(" W25Q64FV QuadSPI Test OK\r\n");
        std::string message="W25Q64FV QuadSPI Test OK\\r\\n";
        HAL_UART_Transmit(&huart1,(uint8_t *)message.data(),message.size(),200);
    }else{
        std::string message="W25Q64FV QuadSPI Test False\r\n";
        HAL_UART_Transmit(&huart1,(uint8_t *)message.data(),message.size(),200);
    }
}
